home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / scbench.arc / SIEVE.C < prev    next >
Text File  |  1980-01-01  |  2KB  |  74 lines

  1.  
  2. /*
  3. ** BYTE Sieve Benchmark
  4. ** Version 1 for 8088/8086/80286/80386
  5. ** March 1988
  6. ** Written in BYTE Small-C
  7. ** Based on Small-C by J.E. Hendrix
  8. **
  9. ** This program executes the infamous Eratosthenes Sieve Prime
  10. ** Number Program from BYTE, Jan. 1983.
  11. **
  12. ** Operation:
  13. ** 1. Turn on stopwatch
  14. ** 2. Execute SIEVE for LOOP iterations
  15. ** 3. Turn off stopwatch
  16. ** 4. Report time and number of primes found
  17. ** 5. Exit
  18. **
  19. */
  20.  
  21. #include stdio.h
  22.  
  23. #define size  8190
  24. #define LOOP  100
  25. #define TRUE  YES
  26. #define FALSE NO
  27.  
  28. int tblock[4];        /* Timer holding array */
  29. char flags [size + 1];
  30.  
  31. main()
  32.  
  33. {
  34.     int i, prime, k, count, iter;
  35.  
  36. /* Announce yourself */
  37.     printf("BYTE Sieve Benchmark\n");
  38.  
  39.     printf("%d iterations\n",LOOP);
  40.  
  41. /* Start timer and execute loop */
  42.     gtime(tblock);
  43.     for (iter = 1; iter <= LOOP; iter++)
  44.           {
  45.           count = 0;                              /* prime counter */
  46.           for (i = 0; i <= size; i++)             /* set all flags true */
  47.                 flags [i] = TRUE;
  48.           for (i = 0; i <= size; i++)
  49.                {
  50.                     if (flags [i])                /* found a prime */
  51.                     {
  52.                     prime = i + i + 3;            /* twice index + 3 */
  53. /*                  printf ("\n%d", prime);  */
  54.                     for (k = i + prime; k <= size; k+= prime)
  55.                                  flags [k] = FALSE;  /* kill all multiple */
  56.                     count++;                         /* primes found */
  57.                     }
  58.                 }
  59.       }
  60.       calctim(tblock);
  61.  
  62. /* Report results */
  63.      printf("Results:  (HH:MM:SS:1/100ths)\n");
  64.      printf("Elapsed time: %d:%d:%d:%d\n\n",tblock[0],tblock[1],
  65.              tblock[2],tblock[3]);
  66.  
  67.      printf ("%d primes.\n\n", count);     /* primes found on 100th pass */
  68.  
  69. /* Exit */
  70.      printf("Press RETURN to exit:");
  71.      fgetc(stdin);
  72.      exit(0);
  73.  }
  74.